package com.amazon.model;

import com.amazon.enums.ResultCodeEnum;
import com.amazon.enums.ResultStatusEnum;
import lombok.Data;

@Data
public class CommonResult<T> {

    /**
     * 返回状态码值 200 404 500 等
     */
    private int code;

    /**
     * 返回成功与否标志 SUCCESS、FAILURE
     */
    private String status;

    /**
     * 返回提示信息：成功可以加一些自定义提示、失败可以存放异常信息
     */
    private String message;

    /**
     * 二次确认标记:为1时，页面弹出message信息。为0不弹
     */
    private int confirmFlag = 0;

    /**
     * 返回数据
     */
    private T data;

    public CommonResult() {

    }

    /**
     * 成功返回时使用
     *
     * @param successMsg
     */
    public CommonResult(String successMsg) {
        this.code = ResultCodeEnum.CODE_200.getResultCode();
        this.status = ResultStatusEnum.SUCCESS.getStatus();
        this.message = successMsg;
    }

    /**
     * 成功返回时使用
     *
     * @param resultData
     */
    public CommonResult(T resultData) {
        this.code = ResultCodeEnum.CODE_200.getResultCode();
        this.status = ResultStatusEnum.SUCCESS.getStatus();
        this.data = resultData;
    }

    /**
     * 成功返回时使用
     *
     * @param resultData
     */
    public CommonResult(T resultData,String message) {
        this.code = ResultCodeEnum.CODE_200.getResultCode();
        this.status = ResultStatusEnum.SUCCESS.getStatus();
        this.data = resultData;
        this.message=message;
    }

    /**
     * 异常返回时使用
     *
     * @param codeEnum
     * @param resultMessage
     */
    public CommonResult(ResultCodeEnum codeEnum, String resultMessage) {
        this.code = codeEnum.getResultCode();
        this.status = ResultStatusEnum.FAILURE.getStatus();
        this.message = resultMessage;
    }

    /**
     * 自定义返回数据时使用
     *
     * @param codeEnum
     * @param resultMessage
     */
    public CommonResult(ResultCodeEnum codeEnum, ResultStatusEnum resultStatus, T resultData, String resultMessage) {
        this.code = codeEnum.getResultCode();
        this.status = resultStatus.getStatus();
        this.data = resultData;
        this.message = resultMessage;
    }

    public static <T> CommonResult<T> ok() {
        return new CommonResult<>("");
    }

    public static <T> CommonResult<T> ok(T resultData) {
        return new CommonResult<>(resultData);
    }

    public static <T> CommonResult<T> ok(T resultData, String msg) {
        return new CommonResult<>(resultData, msg);
    }

    public static <T> CommonResult<T> illegalParameter(String msg) {
        return new CommonResult<>(ResultCodeEnum.CODE_400, msg);
    }

    public static <T> CommonResult<T> fail(String msg) {
        return new CommonResult<>(ResultCodeEnum.CODE_500, msg);
    }

}